<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AddNodeRequest;
use App\Model\Admin;
use App\Models\Node;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NodeController extends Controller
{
    # 权限列表显示
    public function index()
    {
        $data = treeLevel(Node::get()->toArray());
        $admin = new Admin();
        return view("admin.node.index",compact("data","admin"));
    }

    # 添加权限显示
    public function create()
    {
        # 获取到顶级权限
        $data = Node::where("pid",0)->where("is_menu","1")->get();
        return view("admin.node.create",compact("data"));
    }

    # 接收添加数据的处理
    public function store(AddNodeRequest $request)
    {
//        dump($request);die;
        # 表单验证
        Node::create($request->except("_token"));
        return redirect( route("admin.node.index"))->with("success","添加权限成功");
    }


    public function show($node)
    {
    }

    # 修改权限显示
    public function edit($node)
    {
        return view("admin.node.edit",compact("node"));
    }

    # 修改数据的处理
    public function update(Request $request, $node)
    {
        # 表单验证
        $data = $this->validate($request,[
            "node_name" => "required",
            "route_name"=> "required",
            "is_menu"   => "required",
            "pid"       => "required"
        ]);
        Node::where("id",$node["id"])->update($data);
        return redirect( route("admin.node.index"))->with("success","修改权限成功");
    }

    # 删除权限
    public function destroy($id)
    {
        //
    }
}
